package com.demo.service.impl;


import java.util.Date;
import java.util.List;

import com.demo.entity.constants.Constants;
import com.demo.entity.enums.AppiontmentStatusEnum;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.enums.UserRoleTypeEnum;
import com.demo.entity.po.Appointment;

import com.demo.entity.po.UserInfo;
import com.demo.entity.query.AppointmentQuery;

import com.demo.entity.query.UserInfoQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.UserInfoMapper;
import com.demo.service.AppointmentService;

import com.demo.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.AppointmentMapper;

import com.demo.entity.enums.PageSize;

/**
 * @Description:咨询预约表serviceImpl
 * @Date:2025-03-01
 * @author：author
*/
@Service("appointmentService")
public class AppointmentServiceImpl implements AppointmentService{

	@Resource
	private AppointmentMapper<Appointment,AppointmentQuery> appointmentMapper;

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<Appointment> findListByParam(AppointmentQuery query) { 
		return this.appointmentMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(AppointmentQuery query){
		return this.appointmentMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<Appointment> findListByPage(AppointmentQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<Appointment> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(Appointment bean) { 
		return this.appointmentMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<Appointment> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.appointmentMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<Appointment> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.appointmentMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据AppointmentId查询
	 */
	public Appointment getAppointmentByAppointmentId(Long appointmentId) { 
		return this.appointmentMapper.selectByAppointmentId(appointmentId);
	}

	/**
	 * 根据AppointmentId更新
	 */
	public Integer updateAppointmentByAppointmentId(Appointment bean, Long appointmentId) { 
		return this.appointmentMapper.updateByAppointmentId(bean,appointmentId);
	}

	/**
	 * 根据AppointmentId删除
	 */
	public Integer deleteAppointmentByAppointmentId(Long appointmentId) { 
		return this.appointmentMapper.deleteByAppointmentId(appointmentId);
	}


	/**
	 * 修改预约状态
	 * @param userId
	 * @param appointmentId
	 * @param status
	 * @param typeEnum
	 */
	@Override
	public void updateStatus(Long userId, Long appointmentId, Integer status, UserRoleTypeEnum typeEnum) {
		// 1. 数据不存在
		Appointment appointmentDB = appointmentMapper.selectByAppointmentId(appointmentId);
		if (appointmentDB == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		// 状态错误
		AppiontmentStatusEnum statusEnum = AppiontmentStatusEnum.getByStatus(status);
		if (statusEnum == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		UserInfo userInfoDB = userInfoMapper.selectByUserId(userId);
		// 普通用户修改信息
		// 不是咨询师 但是这个预约id 数据库信息用户id 不一致 报错
		if (!userInfoDB.getRole().equals(UserRoleTypeEnum.COUNSELOR.getType())&&!appointmentDB.getUserId().equals(userId)) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		// 咨询师 处理的不是预约自己的
		if (userInfoDB.getRole().equals(UserRoleTypeEnum.COUNSELOR.getType())&&
				!appointmentDB.getCounselorId().equals(userId)){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		Appointment updateInfo = new Appointment();
		updateInfo.setStatus(status);
		appointmentMapper.updateByAppointmentId(updateInfo,appointmentId);

	}

	/**
	 * 保存预约信息
	 * @param appointment
	 */
	@Override
	public void saveInfo(Appointment appointment) {
		UserInfo userInfoDB = userInfoMapper.selectByUserId(appointment.getUserId());
		if (userInfoDB == null) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		Appointment appointmentDB=null;
		if (appointment.getAppointmentId()!=null){
			appointmentDB=appointmentMapper.selectByAppointmentId(appointment.getAppointmentId());
		}
		// 已经完成的预约信息 不允许修改
		if (appointment.getAppointmentId()!=null&&appointmentDB!=null
				&&appointmentDB.getStatus().equals(AppiontmentStatusEnum.COMPLETED.getStatus())){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}

		Date curDate = new Date();
		// 新增时查看是否有未完成的预约
		if (appointment.getAppointmentId()==null){
			AppointmentQuery appointmentQuery = new AppointmentQuery();
			appointmentQuery.setUserId(userInfoDB.getUserId());
			appointmentQuery.setUserId(userInfoDB.getUserId());
			appointmentQuery.setStatsArray(new Integer[]{AppiontmentStatusEnum.UNCOMFIRM.getStatus(),AppiontmentStatusEnum.COMFRMED.getStatus()});
			Integer count = appointmentMapper.selectCount(appointmentQuery);
			if (count > Constants.ZERO){
				throw new BusinessException("您在该咨询师有未完成的预约信息，请联系咨询师查询详情");
			}
			appointment.setStatus(Constants.ZERO);
			appointment.setCreateTime(curDate);
			appointment.setUpdateTime(curDate);
			appointmentMapper.insert(appointment);
		}else {
			appointment.setUpdateTime(curDate);
			appointmentMapper.updateByAppointmentId(appointment,appointment.getAppointmentId());
		}
	}

	/**
	 * 删除预约信息
	 * @param appointmentQuery
	 */
	@Override
	public void deleteByParam(AppointmentQuery appointmentQuery) {
		Appointment appointmentDB = appointmentMapper.selectByAppointmentId(appointmentQuery.getAppointmentId());
		// 用户信息 不一致，已经完成的预约都不能删除
		if (appointmentDB == null||appointmentDB.getStatus().equals(AppiontmentStatusEnum.COMPLETED.getStatus())
				||!appointmentDB.getUserId().equals(appointmentQuery.getUserId())) {
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		appointmentMapper.deleteByParam(appointmentQuery);
	}
}